//glad.h必须在glfw3.h之前引入

#include "glframework/shader.h"
#include "glframework/core.h"
#include <iostream>
#include "wrapper/checkErr.h"
#include "application/application.h"

/*
ebo进行绘制
绘制彩色三角形
*/

GLuint vao;
std::unique_ptr<Shader> shader = nullptr;

static void onSize(int width, int height) {
	std::cout << "on size" << std::endl;
}
static void onKey(int key, int action, int mods) {
	if (action) {
		std::cout << "按下了:" << key << ",mods = " << mods << std::endl;
	}
	else {
		std::cout << "弹起了:" << key << ",mods = " << mods << std::endl;
	}
}

void prepareShader() {
	shader = std::make_unique<Shader>("assets/shaders/vertex.glsl","assets/shaders/fragement.glsl");
}

void render() {
	GL_CALL(glClear(GL_COLOR_BUFFER_BIT), "执行画布清理工作");
	//1.绑定当前的program
	shader->begin();
	//2.绑定当前的vao
	glBindVertexArray(vao);
	//3.发出绘制指令
	//glDrawArrays(GL_TRIANGLES,0,6);
	//GL_CALL(glDrawElements(GL_TRIANGLES,3, GL_UNSIGNED_INT,(void*)(sizeof(int)*3)),"执行绘制指令");
	GL_CALL(glDrawElements(GL_TRIANGLES,3, GL_UNSIGNED_INT,0),"执行绘制指令");
	glBindVertexArray(0);
	shader->end();

}

void prepareEbo() {
	float positions[] = {
		-0.5f,-0.5f,0.0f,
		0.5f, -0.5f,0.0f,
		0.0f, 0.5f, 0.0f,
		0.5f, 0.5f, 0.0f
	};
	unsigned int indices[]{
		0,1,2,
		2,1,3
	};

	//1创建vbo
	GLuint vbo;
	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(positions),positions,GL_STATIC_DRAW);

	//2创建ebo	
	GLuint ebo;
	glGenBuffers(1, &ebo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	//3vao创建
	glGenVertexArrays(1, &vao); 
	glBindVertexArray(vao);

	//4绑定vbo ebo加入属性描述信息
	//4.1 加入vbo到当前vao
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glEnableVertexAttribArray(0);//激活0号位置
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3,(void*)0);
	//4.2加入ebo到当前的vao
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);

	glBindVertexArray(0);//解绑当前vao
}

void prepareVAO() {
	//1顶点位置信息
	float positions[] = {
		-0.5f,-0.5f,0.0f,
		0.5f,-0.5f,0.0f,
		0.0f,0.5f,0.0f,
		0.5f, 0.5f, 0.0f
	};
	//2颜色属性信息
	float colors[] = {
		1.0f,0.0f,0.0f,
		0.0f,1.0f,0.0f,
		0.0f,0.0f,1.0f,
	};

	unsigned int indices[]{
		0,1,2,
		2,1,3
	};

	//3.创建vbo
	GLuint posVBO, colorVBO;
	glGenBuffers(1, &posVBO);
	glBindBuffer(GL_ARRAY_BUFFER, posVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);

	glGenBuffers(1, &colorVBO);
	glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);

	//4.创建ebo
	GLuint ebo;
	glGenBuffers(1, &ebo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	//5.创建vao
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);


	//6绑定vbo ebo加入属性描述信息
	//6.1 加入vbo到当前vao
	glBindBuffer(GL_ARRAY_BUFFER, posVBO);
	glEnableVertexAttribArray(0);//激活0号位置
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);

	//6.2加入颜色描述数据
	glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
	glEnableVertexAttribArray(1);//激活1号位置
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);

	//6.3加入ebo到当前的vao
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

	glBindVertexArray(0);//解绑当前vao

}

int main()
{
	if (!app->init()) {
		std::cout << "init fail " << std::endl;
		return -1;
	}

	app->setResizeCallback(onSize);
	app->setKeyBoardCallback(onKey);

	GL_CALL(glViewport(0, 0, 800, 600), "设置视口大小");
	GL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f), "设置画布清理颜色");

	prepareShader();
	//prepareEbo();
	prepareVAO();


	while (app->update()) {
		render();
	}
	 
	if (!app->destroy()) {
		std::cout << "destroy fail" << std::endl;
	}
	return 0;
}